Loading ...
Sorry, an error occurred while loading the content.

Basing default sort direction on column sort options

Expand Messages
  • Mike Prince
    I am in the process of converting a datatable from a working client-side pagination/sorting solution to server-side. One of my existing columns is defined with
    Message 1 of 5 , Jun 26, 2008
    View Source
    • 0 Attachment
      I am in the process of converting a datatable from a working
      client-side pagination/sorting solution to server-side. One of my
      existing columns is defined with a default sort direction of descending:
      {key:"col1", sortable:true,
      sortOptions:defaultDir:YAHOO.widget.DataTable.CLASS_DESC}

      However under Server-side Sorting and Pagination at
      http://developer.yahoo.com/yui/datatable/ the default is hard-coded to
      ascending:

      // Override function for custom server-side sorting
      myTable.sortColumn = function(oColumn) {
      // Default ascending
      var sDir = "asc";

      // If already sorted, sort in opposite direction
      if(oColumn.key === this.get("sortedBy").key) {
      sDir = (this.get("sortedBy").dir ===
      YAHOO.widget.DataTable.CLASS_ASC) ?
      "desc" : "asc";
      }

      // Define the new state
      var newState = {
      start: 0,
      sorting: { // Sort values
      key: oColumn.key,
      dir: (sDir === "desc") ? YAHOO.widget.DataTable.CLASS_DESC
      : YAHOO.widget.DataTable.CLASS_ASC
      },
      pagination : { // Pagination values
      recordOffset: 0, // Default to first page when sorting
      rowsPerPage: this.get("paginator").getRowsPerPage() //
      Keep current setting
      }
      };

      // Create callback object for the request
      var oCallback = {
      success: this.onDataReturnSetRows,
      failure: this.onDataReturnSetRows,
      scope: this,
      argument: newState // Pass in new state as data payload for
      callback function to use
      };

      // Send the request
      this.getDataSource().sendRequest(buildQueryString(newState),
      oCallback);
      };

      How can I amend this function so that the default sort direction will
      be taken from the column-specific sortOptions?

      Cheers
      -- Mike --
    • Mike Prince
      Filed as a (documentation) bug at http://sourceforge.net/tracker/index.php?func=detail&aid=2004907&group_id=165715&atid=836476
      Message 2 of 5 , Jun 28, 2008
      View Source
      • 0 Attachment
      • jennykhan
        Hi Mike, The point is that myTable.sortColumn() is a custom function that you define, so you can set the defaults however you wish. The example here
        Message 3 of 5 , Aug 19, 2008
        View Source
        • 0 Attachment
          Hi Mike,

          The point is that myTable.sortColumn() is a custom function that you
          define, so you can set the defaults however you wish.

          The example here demonstrates how you might go about declaring this
          custom function. You can copy and paste the code as a starting point
          but modify it as needed.

          Please note that in the upcoming release, we are working towards
          simplifying server-side sorting and pagination to make it even easier
          to implement.

          Regards,
          Jenny
        • Mike Prince
          I half-expected this response, although thought that it should actually be pretty easy to do properly. Can anyone tell me how to set sDir based on the
          Message 4 of 5 , Aug 20, 2008
          View Source
          • 0 Attachment
            I half-expected this response, although thought that it should
            actually be pretty easy to do properly.

            Can anyone tell me how to set sDir based on the sortOptions in the
            column definitions for the column in question, i.e. replace the
            hard-coded "asc" in the code below?

            // Override function for custom server-side sorting
            myTable.sortColumn = function(oColumn) {
            // Default ascending
            var sDir = "asc";
            ...

            Cheers
            -- Mike --


            --- In ydn-javascript@yahoogroups.com, "jennykhan" <jennyhan@...> wrote:
            >
            > Hi Mike,
            >
            > The point is that myTable.sortColumn() is a custom function that you
            > define, so you can set the defaults however you wish.
            >
            > The example here demonstrates how you might go about declaring this
            > custom function. You can copy and paste the code as a starting point
            > but modify it as needed.
            >
            > Please note that in the upcoming release, we are working towards
            > simplifying server-side sorting and pagination to make it even easier
            > to implement.
            >
            > Regards,
            > Jenny
            >
          • Mike Prince
            This was actually quite easy: // Set default sort order if (oColumn.sortOptions.defaultDir === YAHOO.widget.DataTable.CLASS_DESC) { var sDir =
            Message 5 of 5 , Aug 22, 2008
            View Source
            • 0 Attachment
              This was actually quite easy:

              // Set default sort order
              if (oColumn.sortOptions.defaultDir === YAHOO.widget.DataTable.CLASS_DESC)
              {
              var sDir = YAHOO.widget.DataTable.CLASS_DESC;
              }
              else
              {
              var sDir = YAHOO.widget.DataTable.CLASS_ASC;
              }
            Your message has been successfully submitted and would be delivered to recipients shortly.